package com.woniu.defecttracking.controller.datas;


import com.woniu.defecttracking.commens.Result;
import com.woniu.defecttracking.dto.RuleDto;
import com.woniu.defecttracking.dto.RulePageDto;
import com.woniu.defecttracking.exception.RuleException;
import com.woniu.defecttracking.param.AddRuleParam;
import com.woniu.defecttracking.param.FindRuleParam;
import com.woniu.defecttracking.param.UpdateRuleParam;
import com.woniu.defecttracking.service.RuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  rule的前端控制器
 * </p>
 *
 * @author 倪西
 * @since 2021-12-31
 */
@RestController
@RequestMapping("/rule")
public class RuleController {

    @Autowired
    private RuleService ruleService;
    
    /**
    * @作者: 倪西
    * @描述：有条件的查询所有的规则返回给前端
    * @时间：2022/1/1 16:42
    * @参数：[]
    * @返回：com.woniu.defecttracking.commens.Result
    **/
    @PostMapping("/findRule")
    public Result findRule(@RequestBody FindRuleParam findRuleParam) throws RuleException {
        RulePageDto rulePage = ruleService.findRule(findRuleParam);
        return Result.getInstance(rulePage).setMessage("查询成功");
    }

    /**
    * @作者: 倪西
    * @描述：获取所有的rule返回给role的页面展示
    * @时间：2022/1/5 20:27
    * @参数：[]
    * @返回：com.woniu.defecttracking.commens.Result
    **/
    @PostMapping("/getRules")
    public Result getRules(){
        List<RuleDto> rules = ruleService.getRules();
        return Result.getInstance(rules).setMessage("获取成功");
    }

    /**
    * @作者: 倪西
    * @描述：新增rule
    * @时间：2022/1/4 17:29
    * @参数：[addRuleParam]
    * @返回：com.woniu.defecttracking.commens.Result
    **/
    @PostMapping("/addRule")
    public Result addRule(@RequestBody AddRuleParam addRuleParam) throws RuleException {
        RuleDto ruleDto = ruleService.saveRule(addRuleParam);
        if (null!=ruleDto){
             return Result.getInstance(ruleDto).setMessage("新增成功");
        }else {
            return Result.getInstance(null).setMessage("新增失败").setCode(411);
        }
    }

    /**
    * @作者: 倪西
    * @描述：修改rule的状态
    * @时间：2022/1/7 21:55
    * @参数：[id]
    * @返回：com.woniu.defecttracking.commens.Result
    **/
    @GetMapping("/changeRule/{id}")
    public Result changeRule(@PathVariable("id") Integer id) throws RuleException {
        Boolean bool = ruleService.changeRule(id);
        if (bool){
            return Result.getInstance(null).setMessage("修改成功");
        }else {
            return Result.getInstance(null).setMessage("修改失败").setCode(422);
        }
    }

    /**
    * @作者: 倪西
    * @描述：删除所选择的rule
    * @时间：2022/1/7 21:55
    * @参数：[ids]
    * @返回：com.woniu.defecttracking.commens.Result
    **/
    @PostMapping("/removeRule")
    public Result removeRule(@RequestBody List<Integer> ids){
        Boolean bool = ruleService.removeRule(ids);
        if (bool){
            return Result.getInstance(null).setMessage("删除成功");
        }else {
            return Result.getInstance(null).setMessage("删除失败").setCode(433);
        }
    }

    /**
    * @作者: 倪西
    * @描述：修改相应的rule信息
    * @时间：2022/1/7 21:56
    * @参数：[updateRuleParam]
    * @返回：com.woniu.defecttracking.commens.Result
    **/
    @PostMapping("/updateRule")
    public Result updateRule(@RequestBody UpdateRuleParam updateRuleParam) throws RuleException {
        Boolean bool = ruleService.updateRule(updateRuleParam);
        if (bool){
            return Result.getInstance(null).setMessage("修改成功");
        }else {
            return Result.getInstance(null).setMessage("修改失败").setCode(422);
        }
    }

    /**
    * @作者: 倪西
    * @描述：通过id获取对应的rule，展示起rule的名称
    * @时间：2022/1/7 21:57
    * @参数：[ids]
    * @返回：com.woniu.defecttracking.commens.Result
    **/
    @PostMapping("/getRulesByIds")
    public Result getRulesByIds(@RequestBody List<Integer> ids) throws RuleException {
        List<RuleDto> rules = ruleService.getRulesByIds(ids);
        StringBuffer str = new StringBuffer();
        for (int i = 0; i <rules.size() ; i++) {
            if (i==rules.size()-1){
                str.append(rules.get(i).getRuleName());
            }else {
                str.append(rules.get(i).getRuleName()+",");
            }
        }
        return Result.getInstance(str).setMessage("查询成功");
    }
}

